// packageA/pages/index/baojiaFaubu/baojiaFabu.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    danwei: [{
        name: '元/顿',
        flag: false
      },
      {
        name: '元/公斤',
        flag: true
      },
      {
        name: '元/克',
        flag: true
      }
    ],
    danweiInput: true,
    danweiIndex: 0,
    show: false,
    showAddress: false,
    CJshow: false,
    classname: '',
    provinceIndex: 0,
    cityIndex: 0,
    CaiJiUserTips: '',
    CJcomType: ['废金属', '再生塑料', '废纸', '铜厂、铜材厂', '调剂商、贸易商', '钢厂、钢铁冶金'],
    CJcomTypeIndex: 0,
    ClassIndex: ""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var that = this
    console.log(options, 'option')
    that.setData({
      classIndex: options.ClassIndex
    })
    this.queryPinming({}, 0)
    this.queryAddress({})
    await that.queryisCJ()

    // this.setData({
    //   personName:wx.getStorageSync('FjLoginLog').UName,
    //   phone:wx.getStorageSync('FjLoginLog').Mobile,
    //   YZMobile:wx.getStorageSync('FjLoginLog').YZMobile,
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectDanwei(e) {
    let index = e.currentTarget.dataset.index,
      dw = this.data.danwei,
      dI = this.data.danweiInput
    dw.forEach(item => {
      item.flag = true
    })
    dw[index].flag = !dw[index].flag
    dI = true
    this.setData({
      danwei: dw,
      danweiInput: dI,
      danweiIndex: index,
      unit:''
    })
  },
  selectDanweiInput(e) {
    let dI = this.data.danweiInput,
      dw = this.data.danwei
    dw.forEach(item => {
      item.flag = true
    })
    dI = false
    this.setData({
      danwei: dw,
      danweiInput: dI
    })
  },
  danweiInputvalue(e) {
    this.setData({
      unit: e.detail.value
    })
  },
  //显示告警列表
  showTypeSelect(e) {
    console.log("列表显示")
    this.setData({
      show: !this.data.show
    })
  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  scroll(e) {
    // console.log(e)
  },
  queryPinming(e, row, index) {
    let that = this,
      classname = ''
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_Get_NextClassByID', e, r => {
      console.log(r, 'baojia')
      if (r.code == 200) {
        if (row == 0) { //第一列
          that.setData({
            pinMing1: r.data
          })
        } else if (row == 1) { //第2列
          if (r.data.length == 0) { //如果第2列为空 拼接第1
            classname = that.data.pinMing1[that.data.pinMing1Index].classname
            this.setData({
              show: false,
              classname
            });
          } else {
            that.setData({
              pinMing2: r.data
            })
          }
        } else if (row == 2) { //第3列
          if (r.data.length == 0) { //如果第三列为空 拼接第1和二列数据
            classname = that.data.pinMing1[that.data.pinMing1Index].classname + ' >> ' + that.data.pinMing2[that.data.pinMing2Index].classname
            this.setData({
              show: false,
              classname
            });
          } else {
            that.setData({
              pinMing3: r.data
            })
          }
        } else if (row == 3) { //第4列
          if (r.data.length == 0) { //如果第4列为空 拼接第1 2 3列数据
            classname = that.data.pinMing1[that.data.pinMing1Index].classname + '>>' +
              that.data.pinMing2[that.data.pinMing2Index].classname + '>>' +
              that.data.pinMing3[that.data.pinMing3Index].classname
            this.setData({
              show: false,
              classname
            });
          } else {
            that.setData({
              pinMing4: r.data
            })
          }
        } else if (row == 4) { //第5列
          if (r.data.length == 0) { //如果第5列为空 拼接第1234列数据
            classname = that.data.pinMing1[that.data.pinMing1Index].classname + '>>' +
              that.data.pinMing2[that.data.pinMing2Index].classname + '>>' +
              that.data.pinMing3[that.data.pinMing3Index].classname + '>>' +
              that.data.pinMing4[that.data.pinMing4Index].classname
            this.setData({
              show: false,
              classname
            });
          } else {
            that.setData({
              pinMing5: r.data
            })
          }
        }
      }
    })
  },

  cSelect1(e) {
    let classid = e.currentTarget.dataset.classid,
      index = e.currentTarget.dataset.index,
      row = e.currentTarget.dataset.row
    if (row == 1) {
      this.setData({
        pinMing1Index: index,
        pinMing2Index: -1,
        pinMing3Index: -1,
        pinMing4Index: -1,
        pinMing5Index: -1
      })
    } else if (row == 2) {
      this.setData({
        pinMing2Index: index,
        pinMing3Index: -1,
        pinMing4Index: -1,
        pinMing5Index: -1
      })
    } else if (row == 3) {
      this.setData({
        pinMing3Index: index,
        pinMing4Index: -1,
        pinMing5Index: -1
      })
    } else if (row == 4) {
      this.setData({
        pinMing4Index: index,
        pinMing5Index: -1
      })
    } else if (row == 5) {
      this.setData({
        pinMing5Index: index
      })
    }
    this.queryPinming({
      ID: classid
    }, row, index)
  },
  guigeInput(e) {
    this.setData({
      guige: e.detail.value
    })
  },
  priceInput(e) {
    this.setData({
      price: e.detail.value
    })
  },
  queryAddress(e) {
    let that = this
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_ProvinceAndCity', e, r => {
      console.log(r, '地区')
      if (r.code == 200) {
        that.setData({
          province: r.data,
        })
      }
    })
  },
  onCloseAddress() {
    this.setData({
      showAddress: false
    })
  },
  selectAddress(e) {
    this.setData({
      showAddress: true
    })
  },
  onCloseCJ() {
    this.setData({
      CJshow: false
    })
  },
  selectProvince(e) {
    let index = e.currentTarget.dataset.index,
      sheng = this.data.province
    if (sheng[index].name.indexOf('全国') != -1) { //如果是全国
      this.setData({
        provinceName: sheng[index].name,
        showAddress: false,
        provinceIndex: index,
        cityName: '',
        city: [],
        cityId: 0,
      })
    } else {
      this.setData({
        provinceName: sheng[index].name,
        city: sheng[index].list,
        provinceIndex: index,
        cityName: ''
      })
    }
  },
  selectCity(e) {
    let index = e.currentTarget.dataset.index,
      shi = this.data.city
    if (shi[index].name.indexOf('不限') != -1) {
      this.setData({
        cityName: '',
        showAddress: false,
        cityIndex: index,
        cityId: 0,
      })
    } else {
      this.setData({
        cityName: shi[index].name,
        cityId: shi[index].pcid,
        cityIndex: index,
        showAddress: false,
      })
    }
  },
  selectComType(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      CJcomTypeIndex: index
    })
  },
  GJcomInput(e) {
    this.setData({
      ComName: e.detail.value
    })
  },
  GJUserPhoneInput(e) {
    this.setData({
      Mobile: e.detail.value
    })
  },
  GJUserNameInput(e) {
    this.setData({
      Linkman: e.detail.value
    })
  },
  liuyanInput(e) {
    this.setData({
      liuyantext: e.detail.value
    })
  },
  async submitBtn(e) {
    let that = this
    await this.queryisCJ()
    if (that.data.isCaiJiUser == 1) {
    if (!this.data.classname) {
      wx.showToast({
        title: '请选择品名',
        icon: 'none'
      })
      return
    } else if (!this.data.guige) {
      wx.showToast({
        title: '请输入规格',
        icon: 'none'
      })
      return
    } else if (!this.data.price) {
      wx.showToast({
        title: '请输入价格',
        icon: 'none'
      })
      return
    } else if (this.data.danweiInput == false && !this.data.unit) {
      wx.showToast({
        title: '请填写单位',
        icon: 'none'
      })
      return
    } else if (this.data.isCaiJiUser == 0) {
      this.setData({
        CJshow: true
      })
    }
    else if (this.data.isCaiJiUser == 2) {
      wx.showToast({
        title: this.data.CaiJiUserTips,
        icon:'none'
      })
    } 
    else { //提交 

      let api_input = {
        classid: that.data.ClassIndex,
        classname: that.data.classname,
        specification: that.data.guige,
        provid: that.data.province[that.data.provinceIndex].pcid ? that.data.province[that.data.provinceIndex].pcid : 0,
        cityid: that.data.cityId ? that.data.cityId : 0,
        price: that.data.price,
        unit: that.data.danweiInput == true ? that.data.danwei[that.data.danweiIndex].name : that.data.unit,
        LoginUID: wx.getStorageSync('FjLoginLog').UID,
      }
      // Feijiu_BJ_ADD_Quotes
      await app.getDaoPost('api/FeijiuApp/Feijiu_BJ_ADD_Quotes', api_input, function (r) {
        console.log(r, '报价发布');
        if(r.code == 200){
          wx.showToast({
            title: r.data,
          })
          setTimeout(()=>{
            // console.log()
            wx.navigateBack()
          },1500)
        }else if(r.code == 500){
          wx.showToast({
            title: r.ExceptionMessage,
            icon:'none'
          })
        }
      })
      }
    }
  },
  // 申请成为采集单位
  applyCJ() {
    // LoginUID 登录用户编号     CompanyType 公司类型名称    Message 留言   CompanyName 公司名  LinkMan 联系人姓名   LinkTel 联系人电话
    if (!this.data.Linkman) {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none'
      })
      return
    } else if (!this.data.Mobile) {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none'
      })
      return
    }else if (this.data.Mobile.length < 11) {
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return
    }
    let input = {
      LoginUID: wx.getStorageSync('FjLoginLog').UID,
      CompanyType: this.data.CJcomType[this.data.CJcomTypeIndex],
      Message: this.data.liuyantext,
      CompanyName: this.data.ComName,
      LinkMan: this.data.Linkman,
      LinkTel: this.data.Mobile
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_BJ_apply_CollectUser', input, r => {
      console.log(r, '采集单位申请')
      if (r.code == 200) {
        wx.showToast({
          title: r.data,
        })
        that.setData({

        })
      } else {
        wx.showToast({
          title: r.message,
          icon: 'none'
        })
      }
    })
  },
  //查询是不是采集单位
  async queryisCJ() {
    let that = this
    if (app.checkFjLogin()) {
      var FjLoginLog = wx.getStorageSync('FjLoginLog');
      var api_input = {
        uid: FjLoginLog.UID,
        upwd: FjLoginLog.UPwd
      }
      //采集单位
      await app.getDaoPost('api/FeijiuApp/Post_FJ_UserInfoByUID', api_input, function (r) {
        var udata = JSON.parse(app.aesDec(r.data));
        console.log(udata, '===采集单位');
        if (udata.isCaiJiUser == 0) { //1 是 0不是没有提交申请 2不是提交过
          that.setData({
            CJshow: true,
          })
        } else if (udata.isCaiJiUser == 2) { //
          wx.showToast({
            title: udata.CaiJiUserTips,
            icon: 'none'
          })

        }
        that.setData({
          ComName: udata.Uinfo[0].ComName,
          Linkman: udata.Uinfo[0].Linkman,
          Mobile: udata.Uinfo[0].Mobile,
          IsGQVideo: udata.IsGQVideo,
          isCaiJiUser: udata.isCaiJiUser,
          CaiJiUserTips: udata.CaiJiUserTips
        })
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/index'
      });
    }
  }
})